package com.fr.report.io.core;

import com.fr.base.FCloneable;
import com.fr.data.Dictionary;
import com.fr.data.TableData;
import com.fr.data.impl.CustomDictionary;
import com.fr.data.impl.EmbeddedTableData;
import com.fr.report.CellElement;
import com.fr.report.Report;
import com.fr.report.TemplateWorkBook;
import com.fr.report.WorkBook;
import com.fr.report.cellElement.core.CellUtils;
import com.fr.report.core.ParameterHelper;
import com.fr.report.io.AbstractEmbExporter;
import com.fr.report.io.TemplateExporter;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class EmbeddedTableDataExporter extends AbstractEmbExporter
{
  public void export(OutputStream paramOutputStream, TemplateWorkBook paramTemplateWorkBook)
    throws Exception
  {
    paramTemplateWorkBook = (WorkBook)paramTemplateWorkBook.clone();
    convertWorkBook(paramTemplateWorkBook);
    new TemplateExporter().export(paramOutputStream, paramTemplateWorkBook);
  }

  private void convertWorkBook(TemplateWorkBook paramTemplateWorkBook)
    throws Exception
  {
    HashMap localHashMap = new HashMap();
    Iterator localIterator1 = paramTemplateWorkBook.getTableDataNameIterator();
    while (localIterator1.hasNext())
    {
      String str = (String)localIterator1.next();
      TableData localTableData = paramTemplateWorkBook.getTableData(str);
      localHashMap.put(str, localTableData);
    }
    int i = 0;
    int j = paramTemplateWorkBook.getReportCount();
    while (i < j)
    {
      convertCellElement(paramTemplateWorkBook.getReport(i), localHashMap);
      ++i;
    }
    Iterator localIterator2 = localHashMap.entrySet().iterator();
    while (localIterator2.hasNext())
    {
      Map.Entry localEntry = (Map.Entry)localIterator2.next();
      paramTemplateWorkBook.putTableData(localEntry.getKey().toString(), EmbeddedTableData.embedify((TableData)localEntry.getValue()));
    }
  }

  private void convertCellElement(Report paramReport, Map paramMap)
  {
    Iterator localIterator = paramReport.cellIterator();
    while (true)
    {
      CellElement localCellElement;
      Dictionary localDictionary;
      do
      {
        if (!(localIterator.hasNext()))
          return;
        localCellElement = (CellElement)localIterator.next();
        ParameterHelper.collectValueTableData(localCellElement.getValue(), paramMap);
        localDictionary = CellUtils.getDictionary(localCellElement);
      }
      while ((localDictionary == null) || (localDictionary instanceof CustomDictionary));
      CellUtils.setDictionary(localCellElement, CustomDictionary.customify(localDictionary));
    }
  }
}